package com.sihuatech.coreagent.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.sihuatech.coreagent.core.communication.entity.RequestSetting;
import com.sihuatech.coreagent.model.response.MPOrderNotifyReq;
import com.sihuatech.coreagent.model.response.MpItem;
import com.sihuatech.coreagent.redis.RedisManager;
import com.sihuatech.coreagent.send.RequestSender;
import com.sihuatech.coreagent.until.XmlParser;

@Service
public class ScspSendService {
	private static final Logger logger = LoggerFactory.getLogger(ScspSendService.class);
	@Autowired
	RedisManager redisManager;
	/**
	 * 发送xml到第三方
	 * @param request
	 * @return
	 */
	public String sendToThird(String xmlString,String interface_type,String proviceId,String platform_code) {
		if(StringUtils.isNotEmpty(xmlString) && StringUtils.isNotEmpty(interface_type)){
			try {
				String returnXml = null;
				RequestSetting requestSettings = redisManager.getInfo(interface_type, platform_code, proviceId);
				String url = requestSettings.getPlatformUrl();
				logger.info("agent请求地址："+url);
				PostMethod post = new PostMethod(url);
				HttpClient httpclient = new HttpClient();
				RequestEntity entity = new StringRequestEntity(xmlString, "application/xml", "UTF-8");
				post.setRequestEntity(entity);
				int result = httpclient.executeMethod(post);
				if (result == HttpStatus.SC_OK) {
					returnXml = new String(post.getResponseBody(), "utf-8");
					}
				return returnXml;
			} catch (Exception e) {
				logger.error("XML post 失败!", e);
			}
		}
		return null;
	}
	
	/**
	 * 发送到下沉省份
	 * @param xmlString
	 * @return
	 */
	public String sendToProvince(String xmlString,String proviceId,String bufiCode,String platform_code) {
		if (StringUtils.isNotEmpty(xmlString) && StringUtils.isNotEmpty(proviceId)) {
				// TODO 根据省份缩写获取省份地址
			    String url = "";
				if(StringUtils.isNotBlank(proviceId) && StringUtils.isNotBlank(bufiCode)) {
					RequestSetting requestSetting = redisManager.getInfo(bufiCode, platform_code, proviceId);
					 url = requestSetting.getPlatformUrl();
				}
				PostMethod post = null;
				String returnXml = null;
				logger.info("agent向"+url+"分省发送的报文为:\r\n" + xmlString);
				try {
					HttpClient httpclient = new HttpClient();
					post = new PostMethod(url);
					RequestEntity entity = new StringRequestEntity(xmlString, "text/xml", "UTF-8");
					post.setRequestEntity(entity);
					httpclient.executeMethod(post);
					if (post.getStatusCode() == HttpStatus.SC_OK) {
						returnXml = post.getResponseBodyAsString();
					}
					return returnXml;
				} catch (Exception e) {
					logger.error("XML post 失败!", e);
				} finally {
					if (null != post) {
						post.releaseConnection();
					}
				}
			}
		return null;
	}
	
}
